package cn.tedu.wms.passport.service.impl;

import cn.tedu.wms.passport.ex.ServiceException;
import cn.tedu.wms.passport.mapper.AdminRoleMapper;
import cn.tedu.wms.passport.pojo.dto.AdminRoleAddNewDTO;
import cn.tedu.wms.passport.pojo.entity.AdminRole;
import cn.tedu.wms.passport.pojo.vo.AdminRoleListItemVO;
import cn.tedu.wms.passport.pojo.vo.AdminRoleStandardVO;
import cn.tedu.wms.passport.service.IAdminRoleService;
import cn.tedu.wms.passport.web.ServiceCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

@Slf4j
@Service
public class AdminRoleServiceImpl implements IAdminRoleService {

    @Autowired
    AdminRoleMapper mapper;

    @Override
    public void addNew(AdminRoleAddNewDTO adminRoleAddNewDTO) {
        Long adminId=adminRoleAddNewDTO.getAdminId();
        Long[] roleIds=adminRoleAddNewDTO.getRoleIds();
        log.info("新增的关联数据管理员id:{},角色id{}",adminId,roleIds);
        List<AdminRole> list =new ArrayList<>();
        for (Long roleId : roleIds) {
            int rows = mapper.countById(adminId, roleId);
            if (rows != 0) {
                String message = "新增失败,该管理员关联的角色id:"+roleId+"已存在";
                log.info(message);
                throw new ServiceException(ServiceCode.ERR_CONFLICT, message);
            }
            AdminRole adminRole=new AdminRole(adminId, roleId);
            list.add(adminRole);
        }
        int rows = mapper.insertBatch(list);
        if(rows!=roleIds.length){
            String message="服务器忙,请稍后重试";
            log.info(message);
            throw new ServiceException(ServiceCode.ERR_INSERT,message);
        }
    }

    @Override
    public void insertBatch(List<AdminRoleAddNewDTO> addNewList) {

    }

    @Override
    public void deleteById(Long adminId) {
        int rows = mapper.countByAdminId(adminId);
        if(rows<1){
            String message="删除失败,管理员角色关联数据不存在";
            log.info(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,message);
        }
        rows = mapper.deleteByAdminId(adminId);
        if(rows<1){
            String message="服务器忙,请稍后重试";
            log.info(message);
            throw new ServiceException(ServiceCode.ERR_DELETE,message);
        }
    }

    @Override
    public void updateById(AdminRoleStandardVO adminRoleStandardVO) {
        int rows = mapper.countByAdminRoleId(adminRoleStandardVO.getId());
        if(rows!=1){
            String message="修改失败,管理员角色关联数据不存在";
            log.info(message);
            throw new ServiceException(ServiceCode.ERR_UPDATE,message);
        }
        AdminRole adminRole =new AdminRole();
        BeanUtils.copyProperties(adminRoleStandardVO,adminRole);
        rows = mapper.updateById(adminRole);
        if(rows<1){
            String message="服务器忙,请稍后重试";
            log.info(message);
            throw new ServiceException(ServiceCode.ERR_UPDATE,message);
        }

    }

    @Override
    public void countByAdminId(Long adminId) {

    }

    @Override
    public AdminRoleStandardVO getStandardById(Long id) {
        AdminRoleStandardVO queryResult = mapper.getStandardById(id);
        if(queryResult==null){
            String message="查询失败,数据不存在";
            log.info(message);
            throw new ServiceException(ServiceCode.ERR_SELECT,message);
        }
        return queryResult;
    }

    @Override
    public List<AdminRoleListItemVO> list() {
        List<AdminRoleListItemVO> list = mapper.list();
        Iterator<AdminRoleListItemVO> iterator = list.iterator();
        while (iterator.hasNext()) {
            AdminRoleListItemVO item=iterator.next();
            if (item.getAdminId()==1) {
                iterator.remove();
                break;
            }
        }
        return list;
    }
}
